# -*- coding: utf-8 -*-

import sys
sys.path.append('/Users/xbs/Code/HunterQuant')
from datetime import datetime
from data.basic_crawler import BasicCrawler
from data.daily_crawler import DailyCrawler
#from data.finance_report_crawler import FinanceReportCrawler
from data.fixing.daily_fixing import DailyFixing
from strategy.strategy_module import Strategy
from factor.factor_module import FactorModule
from util.stock_util import get_trading_dates
from trading.signal.computer.signal_module  import SignalModule
import time
from trading.livetrading import LiveTrading
from monitor.monitor_module import MonitorModule
# 获取当前日期
all_dates = get_trading_dates()
#begin_date = all_dates[-1]
begin_date = "2000-01-01"
current_date = datetime.now().strftime('%Y-%m-%d')





def crawl_data():
    """
    抓取数据
    先抓取daily的交易数据，然后basic数据抓取时才能获得上证指数的交易日期，再按照交易日期去获取basic数据；
    另外，先判断抓取数据的起始时间到数据库数据的最新时间之间是否存在缺失的交易数据，如果存在先补充数据；
    """
    #
    start_time = time.time()
    bc = BasicCrawler()
    dc = DailyCrawler()

    #获取已记录在数据库当前日期向前一年的交易日期，从数据库最后一个交易日期开始补充数据到当前日期


    #顺序不能变，先抓取指数数据后，才有上证交易日期，才能抓个股数据
    print("开始抓取数据的时间：%s,结束时间：%s" % (begin_date,current_date))
    #dc.crawl_index(begin_date=begin_date, end_date=current_date)
    #dc.multi_crawl_codes(begin_date=begin_date, end_date=current_date)
    
    ##bc.crawl_basic(begin_date=begin_date, end_date=current_date)
    
    bc.crawl_stable_data()
    bc.crawl_index_cons()


    ##bc.crawl_tdx_daily_data()
    ##bc.crawl_choice_daily_data()



    #
    # fc = FinanceReportCrawler()
    #
    # fc.crawl_finance_report()
    # crawl_finance_report_time = time.time()
    # fc.crawl_finance_summary()
    # crawl_finance_summary_time = time.time()


def fixing_data():
    """
    修复数据
    """
    #获取已记录在数据库当前日期向前一年的交易日期，从数据库最后一个交易日期开始补充数据到当前日期
    df = DailyFixing()

    # 填充缺失的k线，复权因子，前收
    df.multi_fixing(index=False, begin_date=begin_date,end_date=current_date)
    df.multi_fixing(index=True, begin_date=begin_date,end_date=current_date)





def compute_factor():
    """
    计算所有因子
    """
    fc = FactorModule()
    fc.compute(begin_date, current_date)

def compute_signal():
    """
    计算所有信号
    """
    sc = SignalModule()
    sc.compute(begin_date, current_date)

def get_today_candidates():
    """
    通过回测获得今天的备选股
    """
    strategy = Strategy('trending_strategy')
    strategy.backtest()

def import_live_traing_data():
    live_trading = LiveTrading(305000,'2017-10-10')
    live_trading.import_trading_data()
    live_trading.process_trading_record()
    live_trading.process_date_holding()

def monitor():
    mt = MonitorModule()
    mt.monitoring(begin_date,current_date)

if __name__ == '__main__':
    start_time = time.time()
    crawl_data()
    crawl_time = time.time()
    fixing_data()
    fixing_time = time.time()
    #compute_factor()
    factor_time = time.time()
    #compute_signal()
    signal_time = time.time()
    #monitor()
    mt_time =time.time()
    #import_live_traing_data()
    import_trading_time = time.time()

    print(f"获取数据耗时：{round(crawl_time - start_time,2)}秒，修复数据耗时：{round(fixing_time - crawl_time,2)}秒，"
          f"计算因子耗时：{round(factor_time - fixing_time,2)}秒, 计算信号耗时：{round(signal_time - factor_time,2)}秒"
          f"导入交易数据耗时：{round(mt_time-signal_time,2)}秒"
          f"计算监控数据耗时：{round(import_trading_time-mt_time,2)}秒")
    # #get_today_candidates()

